<?

require_once("mysql.php");
require_once("suggest.php");

$search = strtolower($_GET['search']);

$search = ESC($search);

$simple = str_replace(".com", "", $search);

$result = array();

$q = "SELECT name, online FROM sellerinfo WHERE name LIKE 'XXX%'"; 
$q = str_replace('XXX', $simple, $q); 
$r = MYSQL_Q($q);
if (!$r) die("[]");

while ($seller = mysql_fetch_assoc($r)) {
    array_push($result, $seller);
}

/*
 * If we don't have any hits in our local database of retailers, look it up in 
 * Google to get some suggestions... 
 */
if (count($result) < 3) {
    $g = google_suggest($simple);

    foreach($g as $s) {
        // TODO: suppress duplicates
        array_push($result, $s);
    }
}


$result = array_slice($result, 0, 3);

echo json_encode($result, true);


?>
